package com.ysqcould.approval.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.ysqcould.allocation.domain.WxUser;
import com.ysqcould.allocation.mapper.WxUserMapper;
import com.ysqcould.approval.domain.Synergy;
import com.ysqcould.approval.domain.vo.SynergyVo;
import com.ysqcould.approval.mapper.SynergyMapper;
import com.ysqcould.approval.service.ISynergyService;
import com.ysqcould.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 协同办公Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-06-29
 */
@Service
public class SynergyServiceImpl implements ISynergyService
{
    @Autowired
    private SynergyMapper synergyMapper;

    @Autowired
    private WxUserMapper userMapper;
    /**
     * 查询协同办公
     * 
     * @param id 协同办公主键
     * @return 协同办公
     */
    @Override
    public SynergyVo selectSynergyById(Long id)
    {

        SynergyVo synergy = synergyMapper.selectSynergyById(id);
        if (ObjectUtil.isNotEmpty(synergy.getPcUrl()) ){
            synergy.setPcUrlList(synergy.getPcUrl().split(","));
        }
        return synergy;
    }

    /**
     * 查询协同办公列表
     * 
     * @param synergy 协同办公
     * @return 协同办公
     */
    @Override
    public List<SynergyVo> selectSynergyList(Synergy synergy)
    {

        List<SynergyVo> synergyVos = synergyMapper.selectSynergyList(synergy);
        synergyVos.forEach(p->{
            if (ObjectUtil.isNotEmpty(p.getPcUrl())){
                p.setPcUrlList(p.getPcUrl().split(","));
            }
            List<String>list=new ArrayList<>();
          if (ObjectUtil.isNotEmpty(p.getReceiveIds())){
              String[] split = p.getReceiveIds().split(";");
              for (String s : split) {
                  WxUser wxUser = userMapper.selectWxUserById(Long.parseLong(s));
                  if (ObjectUtil.isNotEmpty(wxUser)){
                      list.add(wxUser.getRealName());
                  }
              }
              if (list.size()>0){
                  p.setApplyName(list.stream().collect(Collectors.joining(",")));
              }
          }
        });
        return synergyVos;
    }

    /**
     * 新增协同办公
     * 
     * @param synergy 协同办公
     * @return 结果
     */
    @Override
    public int insertSynergy(Synergy synergy)
    {
        synergy.setCreateTime(DateUtils.getNowDate());
        return synergyMapper.insertSynergy(synergy);
    }

    /**
     * 修改协同办公
     * 
     * @param synergy 协同办公
     * @return 结果
     */
    @Override
    public int updateSynergy(Synergy synergy)
    {
        synergy.setUpdateTime(DateUtils.getNowDate());
        return synergyMapper.updateSynergy(synergy);
    }

    /**
     * 批量删除协同办公
     * 
     * @param ids 需要删除的协同办公主键
     * @return 结果
     */
    @Override
    public int deleteSynergyByIds(Long[] ids)
    {
        return synergyMapper.deleteSynergyByIds(ids);
    }

    /**
     * 删除协同办公信息
     * 
     * @param id 协同办公主键
     * @return 结果
     */
    @Override
    public int deleteSynergyById(Long id)
    {
        return synergyMapper.deleteSynergyById(id);
    }
}
